package com.like.hospitalworld;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.like.dialog.ImgDialogFragment;
import com.like.entity.EventWrapper;
import com.like.entity.GeneralResponse;
import com.like.entity.InfoDetailReq;
import com.like.entity.InfoDetailResp;
import com.like.entity.MeetingDetailResp;
import com.like.likeutils.network.RetrofitCallback;
import com.like.network.InfoService;
import com.like.network.RetrofitUtil;
import com.like.utils.Constants;
import com.like.utils.DateConverter;

import org.greenrobot.eventbus.EventBus;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import retrofit2.Call;
import retrofit2.Response;

@ContentView(R.layout.activity_meeting_detail)
public class MeetingDetailActivity extends BaseActivity {

    private int mMeetingId;
    private MeetingDetailResp mMeetingDetailResp;

    private InfoService mInfoService;

    @ViewInject(R.id.webview_meeting_detail)
    private WebView mWVMeetingDetail;
    @ViewInject(R.id.title)
    private TextView mTitle;
    @ViewInject(R.id.time)
    private TextView mTime;
    private InfoDetailResp mInfoDetailResp;
    private ImgDialogFragment mImgDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mInfoService = RetrofitUtil.getService(InfoService.class);
        mImgDialog = new ImgDialogFragment();
        initWebView();
        mMeetingId = getIntent().getIntExtra("meetingId", -1);
        if (mMeetingId != -1) {
            getMeetingDetail();
        }
    }

    private void addImgClickListener() {
        mWVMeetingDetail.loadUrl(Constants.IMG_CLICK_JS);
    }

    private void initWebView() {
        mWVMeetingDetail.getSettings().setJavaScriptEnabled(true);
        mWVMeetingDetail.addJavascriptInterface(new JavascriptInterface(), "imageListner");
        mWVMeetingDetail.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                addImgClickListener();
            }
        });
    }

    public class JavascriptInterface {
        @android.webkit.JavascriptInterface
        public void openImage(String url) {
            mImgDialog.setImgUrl(url);
            mImgDialog.show(getSupportFragmentManager(), "img_dialog");
        }
    }

    @Event(R.id.courseware)
    private void courseClick(View v) {
        Intent intent = new Intent(m.mContext, CoursewareActivity.class);
        intent.putExtra("meetingId", mMeetingId);
        startActivity(intent);
    }

    @Event(R.id.video)
    private void videoClick(View v) {
        Intent intent = new Intent(m.mContext, VideoActivity.class);
        intent.putExtra("meetingId", mMeetingId);
        startActivity(intent);
    }

    private void getMeetingDetail() {
        InfoDetailReq req = new InfoDetailReq(mMeetingId, m.getLoginResp().userID);
        Call<GeneralResponse<InfoDetailResp>> call = mInfoService.getInfoDetail(Constants.getGUID(), m.getToken(), req);
        call.enqueue(new RetrofitCallback<GeneralResponse<InfoDetailResp>>() {
            @Override
            public void onResponse(Call<GeneralResponse<InfoDetailResp>> call, Response<GeneralResponse<InfoDetailResp>> response) {
                mInfoDetailResp = response.body().data;
                mTime.setText(DateConverter.convertShitToDate(mInfoDetailResp.updatetime, "yyyy-MM-dd HH:mm:ss"));
                mTitle.setText(mInfoDetailResp.title);
                EventBus.getDefault().post(new EventWrapper(mInfoDetailResp, MeetingDetailActivity.class, InfoDetailActivity.REQUEST_INFO_DATA));
                mWVMeetingDetail.loadDataWithBaseURL(null, Constants.FONT_STYLE_START + mInfoDetailResp.textInfo + Constants.FONT_STYLE_END, "text/html", "utf-8", null);
            }

            @Override
            public void onFailure(Call<GeneralResponse<InfoDetailResp>> call, Throwable t) {
                t.printStackTrace();
            }
        });
    }
}
